<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/12/30
 * Time: 10:22
 */

namespace common\components\ActionsComponents;

use common\components\DataHelper;
use common\models\Merchant\Merchant;

class DelAction extends CommonAction
{
    public $message = '删除成功！';
    public $isHard = false;

    public function run()
    {
        $model = new $this->model();
        $controller = $this->controller;

        $delFunc = $this->isHard ? "delByPkForHard" : "delByPk";
        if ( $model->$delFunc(@$this->_request[$this->_pk], $this->condition) ) {
            $response = $controller->jsonResponse($controller::RESPONSE_SUCCESS, $this->message, $this->data);

            if ( $this->isPushLocal ) {
                DataHelper::local_del_table(Merchant::getCurrentMerInfo("id"), $model::tableName(), [
                    $model::primaryKey()[0] => @$this->_request[$this->_pk]
                ]);
            }

            if ( $this->callback instanceof \Closure ) {
                $callback = $this->callback;
                $callback(@$this->_request[$this->_pk], $this->condition);
            }

        } else {
            $response = $controller->jsonResponse($controller::RESPONSE_FAILURE, $model->getFirstErrorToStr(), $model->getFirstErrors());
        }
        return $response;
    }
}